import http = require("http")
import url = require("url")
import GlobDef from "../Module/GlobDef";
import LogFile from "../Module/LogFile";
import EventCenter, { ZPacket } from "./EventIO";

export default class HttpServer{
    // public static pHS: HttpServer = null;
    // public static getInstance(): HttpServer {
    //     if (HttpServer.pHS == null) {
    //         HttpServer.pHS = new HttpServer();
    //     }
    //     return HttpServer.pHS;
    // }

    public m_pServer:http.Server    = null;

    public StartHttpList(port:number){
        this.m_pServer = http.createServer(this.onHttp.bind(this));
        this.m_pServer.listen(port);
        LogFile.getInstance().CheckFileReset();
    }


    public onHttp(req:http.IncomingMessage,res:http.ServerResponse):void{
        var urlstr:string = req.url;
        var vInMsg = GlobDef.SplitCfgString(urlstr,"&");
        //第一位是方法 
        if(vInMsg.length <= 2)
            return; //屏蔽掉非正常包
        console.log(vInMsg);
        var pP = ZPacket.Create(vInMsg);
        pP.pClient  = res;
        EventCenter.getInstance().DispatchPack(pP);

        // console.log(this.m_pMySql);
        // var _url = url.parse(urlstr);

        // this.m_pMySql.query("select * from tba ", function (error, results, fields) {
        //     if (error) throw error;
        //     console.log(results);
        //     //console.log(fields);
        //  });
        // console.log("fire onhttp");
        // var p = new tmp;
        // console.log(urlstr);
        // LogFile.getInstance().OutLog(urlstr);
        // LogFile.getInstance().OutError("msgin");

        // res.write(urlstr);//JSON.stringify(p));
        // res.end();
    }
}